import { defineStore } from "pinia";
import { ref } from "vue";
import { https } from "@/api/api";
import { showToast } from "@/public/index";
const bg_url = import.meta.env.VITE_BASE_URL;
const useMarkerStore = defineStore("marker", () => {
  // 选中的marker
  const marker = ref<MarKerDetail>({
    alias: "",
    toponym: "",
    showsize: "0",
    notes: "",
    phone: "",
    name: "",
    iscreator: "",
    mid: "",
    city: "",
    district: "",
    province: "",
  });
  const picList = ref<Array<Files>>([]);
  const markersList = ref<Array<ListItem>>([]);
  // 可操作的列表
  const operational = ref<Array<ListItem>>([]);
  // 要添加的marker
  const prepareToAddMarker=ref<lngLatde>({});
  // 获取标记点信息
  const getMarkerInfo = async (id: string) => {
    uni.showLoading({
      title: "标记点信息获取中...",
    });
    const res = await https({
      mode: "marker",
      name: "apiGetMarker",
      data: {
        mid: id,
      },
    });
    uni.hideLoading();
    if (res.code !== 0) {
      showToast(res.data.msg);
      return {
        code: 1,
      };
    }
    marker.value = {
      ...res.data.info,
      showsize: res.data.info.showsize + "",
    };
    setPicList(res.data?.imgs);
    return res.code;
  };
  // 更新marker
  const initMarker = () => {
    marker.value = {
      alias: "",
      toponym: "",
      showsize: "0",
      notes: "",
      phone: "",
      name: "",
      iscreator: "",
      mid: "",
      city: "",
      district: "",
      province: "",
    };
  };
  // 设置
  const setMarkerItem = (obj: MarKerDetail) => {
    marker.value = obj;
  };
  // 更新单个
  const updataMarker = (obj: MarKerDetail) => {
    marker.value = {
      ...marker.value,
      ...obj,
    };
  };
  // 设置 pic 图片
  const setPicList = (data: Array<string>) => {
    picList.value = [];
    data?.forEach((item: string) => {
      picList.value.push({
        url: bg_url + "img/marker/" + item,
        // url: bg_url + "img/followup/" + item,
      });
    });
  };
  return {
    marker,
    getMarkerInfo,
    updataMarker,
    setMarkerItem,
    initMarker,
    picList,
    setPicList,
    markersList,
    prepareToAddMarker,
    operational
  };
});
export const useMap = defineStore("map", () => {
  const mapCtx = ref();
  const isRefresh = ref<boolean>(false);
  const setMapCtx = (map: any) => {
    mapCtx.value = map;
  };
  // 创建 marker
  const formatMarker = (item: lngLatde): MarkerItem => {
    return {
      latitude: Number(item.lat) as number,
      longitude: Number(item.lng) as number,
      callout: {
        content: item.alias as string,
        display: "BYCLICK",
        bgColor: "#E8F3FF",
        padding: 5,
        fontSize: 14,
        color: "#275EFF",
        borderWidth: 1,
        borderColor: "#275EFF",
      },
      id: item.mid as number,
      width: "22",
      height: "30",
      zIndex: 1000,
      joinCluster:true,
    };
  };
  // 添加标记点
  const addMarker = (item: lngLatde) => {
    mapCtx.value.addMarkers({
      markers: [formatMarker(item)],
    });
  };
  const updataRefresh = (value: boolean) => {
    isRefresh.value = value;
  };
  return {
    mapCtx,
    isRefresh,
    setMapCtx,
    formatMarker,
    addMarker,
    updataRefresh,
  };
});
export default useMarkerStore;
